/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.generalfile.servlet;

import cn.net.greenet.common.db_service.ConfigManager;
import cn.net.greenet.common.policy_service.PolicySupport;
import cn.net.greenet.db.HibernateUtil;
import cn.net.greenet.db.bean.Iprangeinfo;
import cn.net.greenet.db.bean.Iprangeuser;
import cn.net.greenet.generalfile.logic.DataChange;
import cn.net.greenet.generalfile.logic.GeneralFileProcess;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author Administrator
 */
@WebServlet(name = "GetIpUser2DB", urlPatterns = {"/GetIpUser2DB"})
public class GetIpUser2DB extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
//        String path = request.getParameter("path");
//        String MessageType = request.getParameter("MessageType");
//        String policy_instance = request.getParameter("policy_instance");
//
//        boolean result = false;
//        GeneralFileProcess pro = new GeneralFileProcess(path, MessageType, policy_instance);
//        String name = ConfigManager.getInstance().getString("ipUserFileName");
//        path = pro.saveFile(request, name);
//        if (path != null) {
//            List<String> list = pro.getContent(path, 4);
//            if (!list.isEmpty()) {
//                SessionFactory sf = HibernateUtil.newSessionFactory();
//                Session session = sf.openSession();
//                session.clear();
//                Transaction tx = session.beginTransaction();
//                Iprangeuser iprangeUser = null;
//                try {
//                    for (String line : list) {
//                        String[] ss = line.split("\t");
//                        String user = ss[0];
//                        String type = ss[1];
//                        String ip = ss[2];
//                        String prifex = ss[3];
//                        iprangeUser = new Iprangeuser(user);
//                        int id = DataChange.hex2dec(type);
//                        iprangeUser.setType(id);
//                        iprangeUser.setIp(ip);
//                        iprangeUser.setPrefix(Integer.parseInt(prifex));
//                        session.save(iprangeUser);
//                    }
//                    tx.commit();
//                    result = true;
//                } catch (Exception e) {
//                    tx.rollback();
//                    result = false;
//                } finally {
//                    session.close();
//                    sf.close();
//                }
//                PolicySupport policySupport = PolicySupport.getInstance();
//                String policy = policySupport.getApi().getMessage("0xCA", "默认库", 0).getSvnFile().getContent();
//                String comment = policySupport.getApi().getMessage("0xCA", "默认库", 0).getSvnFile().getComment();
//                policySupport.getApi().updatePolicy("0xCA", "默认库", 0, policy, comment);
//
//            }
//        }
//        try (PrintWriter out = response.getWriter()) {
//            /* TODO output your page here. You may use following sample code. */
//            out.println(result);
//        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
